package com.ykdl.member.kid.doctor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ISaveUserInfoForAutoLoginSuccess;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;

import android.R.bool;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.ChatMsgViewAdapter;
import com.ykdl.member.kid.beans.AskDoctorBeanListBean;
import com.ykdl.member.kid.beans.DoctorAnswerInfoListBean;
import com.ykdl.member.kid.doctor.AskDoctorActivity.getDoctorQuestionsListTag;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.gears.ShareWeiBo;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.util.AccessTokenKeeper;
import com.ykdl.member.kid.util.ChatMsgEntity;
import com.ykdl.member.kid.util.WeiXinUtil;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;
/**
 * 问题详情Acivity
 * @author chenshuai
 */
public class ProblemDetailsAcivity extends BaseScreen implements OnClickListener{
	private ChatMsgViewAdapter adapter ;
	private Dialog share_dialog ;
	private RefreshListView chat_list ;
	private LinearLayout toolbar ;
	private View foot = null;
	private String topicsid ="367";
	private ImageButton ask_thedoctor ;
	private String TAG = "ChatMsgViewAdapter";
	private AccessTokenKeeper mAccessTokenKeeper ;
	private String coment;
	private TextView notice;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("问题详情", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, R.drawable.askdoctor_share_icon, new OnClickListener() {
			@Override
			public void onClick(View v) {
				showShareDialog();
			}
		}, R.drawable.title_bg, false);
		setContent(R.layout.problemdetailsacivity_xml);
		mAccessTokenKeeper = new AccessTokenKeeper(this);
		topicsid = this.getIntent().getStringExtra(KidAction.DOCTOR_TOPICI_DD);
		inint();
	}
	public void inint() {
		notice =  (TextView) findViewById(R.id.notice);
		toolbar =  (LinearLayout) findViewById(R.id.share_bg);
		chat_list = (RefreshListView)findViewById(R.id.chat_list);
		ask_thedoctor = (ImageButton)findViewById(R.id.ask_thedoctor);
		findViewById(R.id.ask_thedoctor).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				go(ToAskDoctorActivity.class);
				Intent intent = new Intent(ProblemDetailsAcivity.this,
						ToAskDoctorActivity.class);
				intent.putExtra("topic_id", topicsid);
				intent.putExtra("again_ask", "1");
				ProblemDetailsAcivity.this.startActivity(intent);
			}
		});
//		findViewById(R.id.share_weibo).setOnClickListener(this);
//		findViewById(R.id.share_weixin).setOnClickListener(this);
//		findViewById(R.id.share_pengyouquan).setOnClickListener(this);
//		findViewById(R.id.share_qq).setOnClickListener(this);
//		findViewById(R.id.share_cancel).setOnClickListener(this);
//		foot = LayoutInflater.from(mContext).inflate(
//				R.layout.problemdetails_foot, null);
//		chat_list.addFooterView(foot);
		adapter = new ChatMsgViewAdapter(this);
    	 inintdata();
	}
	public void inintdata() {
		showProgress("正在发送请稍等...");
		String  pageurl =KidConfig.GET_DOCTOR_TOPICS+topicsid;
		Map<String,String> map = new HashMap<String, String>();
		map.put("sort", "2");
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,"");
		 TaskManager.startHttpRequest(request, new getProblemDetailsListTag(),  DoctorAnswerInfoListBean.class);
	}
	
	class getProblemDetailsListTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof DoctorAnswerInfoListBean) {
				DoctorAnswerInfoListBean bean = (DoctorAnswerInfoListBean)t;
				adapter.setList(bean.getList());
				chat_list.setAdapter(adapter);
				chat_list.setMoreButtoIsGon(true);
				adapter.notifyDataSetChanged();
				Toast.makeText(mContext, "成功", 1).show();
				coment = bean.getTopic().getTitle();
				if ((bean.getTopic().getPost_actor().getActor_id()+"").equals(mAccessTokenKeeper.getMactor_id())) {
					ask_thedoctor.setVisibility(View.VISIBLE);
					notice.setVisibility(View.GONE);
				}else {
					ask_thedoctor.setVisibility(View.GONE);
					notice.setVisibility(View.VISIBLE);
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}

	public void isShareDialog() {
		if (toolbar.getVisibility()==View.VISIBLE) {
			toolbar.setVisibility(View.GONE);
		}else {
			toolbar.setVisibility(View.VISIBLE);
		}
	}
	public void showShareDialog() {
		
		share_dialog = new Dialog(this, R.style.bubble_dialog);
		share_dialog.setCanceledOnTouchOutside(true);
		Window dialogWindow = share_dialog.getWindow();
		WindowManager manager = this.getWindowManager();
		LayoutParams params = dialogWindow.getAttributes();// 获取LayoutParams
		dialogWindow.setGravity(Gravity.BOTTOM);
		params.width = manager.getDefaultDisplay().getWidth();
		dialogWindow.setAttributes(params);
		share_dialog.setContentView(R.layout.share_askdoctor);
		share_dialog.show();
		share_dialog.findViewById(R.id.share_weibo).setOnClickListener(this);
		share_dialog.findViewById(R.id.share_weixin).setOnClickListener(this);
		share_dialog.findViewById(R.id.share_pengyouquan).setOnClickListener(this);
		share_dialog.findViewById(R.id.share_qq).setOnClickListener(this);
		share_dialog.findViewById(R.id.share_cancel).setOnClickListener(this);
		
	}
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
	private void initWeiBo(String coment) {
		String sharecontent = null ;
		try {
			Intent t = new Intent(this, ShareWeiBo.class);
			sharecontent = "我在辣妈育儿，快来围观吧！" + coment
					+ "，【苹果、安卓手机免费下载】http://www.ykdllmyr.com/lmyr/apk/lmyr_latest.apk";
			t.putExtra(KidAction.SHARE_TIP, sharecontent);
			startActivity(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void initWeiXin(String coment,boolean isTogroup) {
//		IWXAPI api = WeiXinUtil.getWXAPIFactoryAndReg(this);
//		int supportAPI = api.getWXAppSupportAPI();
//		if (supportAPI==0) {
//			Toast.makeText(mContext, "未安装微信！", 1).show();
//		}else {
//			WeiXinUtil weixin = new WeiXinUtil(this, api);
//		    weixin.shareWXWebToGroup("我在辣妈育儿，快来围观吧！",coment, null,null,isTogroup);
//		}
//		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.share_weibo:
			initWeiBo(coment);
			break;
		case R.id.share_weixin:
			initWeiXin(coment,false);
			break;
		case R.id.share_pengyouquan:
			initWeiXin(coment,true);
			break;
		case R.id.share_cancel:
			share_dialog.dismiss();
			break;
			
		default:
			break;
		}
	}
}
